<template>
  <div class="container">
    <div class="query-container">
      <el-card class="query-card">
        <el-form
          :model="queryForm"
          class="query-form"
          ref="formRef"
          label-width="120px"
        >
          <el-row>
            <el-col :span="5">
              <el-form-item label="分类名称">
                <el-input v-model="queryForm.name" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="状态">
                <el-select v-model="queryForm.status" placeholder="请选择">
                  <el-option
                    v-for="item in statusList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="14">
              <el-form-item label="">
                <el-button type="primary" @click="handleSearch()"
                  >查询</el-button
                >
                <el-button @click="reset()">重置</el-button>
                <el-button type="warning" @click="handleAdd()">新增</el-button>
                <el-button type="danger" @click="batchDelete()">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
    <div class="table-container">
      <el-table
        :data="tableData"
        border
        fit
        @selection-change="handleSelectionChange"
        size="mini"
        style="width: 100%"
      >
        <el-table-column fixed type="selection" width="55" />
        <el-table-column
          fixed
          prop="id"
          label="ID"
          width="80"
        ></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="remark" label="描述"></el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status == 1 ? 'success' : 'danger'">{{
              scope.row.status == 1 ? "启用" : "禁用"
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-edit"
              @click="handleEdit(scope.row.id)"
              >编辑</el-button
            >
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[1, 10, 20, 50, 100]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { softwareCategory } from "@/api/softwareCategory";
import userService from "@/api/user";
export default {
  data() {
    return {
      tableData: [],
      page: 1,
      limit: 10,
      count: 0,
      queryForm: {
        name: "",
        remark: "",
        status: "",
      },
      statusList: [
        { id: 1, name: "启用" },
        { id: 0, name: "禁用" },
      ],
    };
  },
  created() {
    this.getSoftwareCategoryList();
    this.getCreators();
  },
  methods: {
    getSoftwareCategoryList() {
      softwareCategory
        .list({ page: this.page, limit: this.limit, ...this.queryForm })
        .then((response) => {
          this.tableData = response.data;
          this.count = response.count;
        });
    },
    getCreators() {
      userService.findAll().then((response) => {
        this.creators = response.data;
      });
    },
    handleSearch() {
      this.getSoftwareCategoryList();
    },
    reset() {
      this.queryForm = {};
      this.getSoftwareCategoryList();
    },
    handleAdd() {
      // 跳转新增页面
      this.$router.push({
        name: "SoftwareCategoryAdd",
      });
    },
    batchDelete() {
      const ids = this.multipleSelection.map((item) => item.id);
      if (ids.length === 0) {
        this.$message({
          message: "请选择要删除的数据",
          type: "warning",
        });
        return;
      }
      const batchDeleteDto = {
        ids: ids,
      };
      // console.log(batchDeleteDto);
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 删除
          opusCategory.deleteBatch(batchDeleteDto).then((response) => {
            this.$message({
              message: response.msg,
            });
            this.getSoftwareCategoryList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
    handleEdit(id) {
      // 跳转编辑页面
      this.$router.push({
        name: "SoftwareCategoryEdit",
        params: { id: id },
      });
    },
    handleDelete(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 删除
          opusCategory.delete(id).then((response) => {
            this.$message({
              message: response.msg,
            });
            this.getSoftwareCategoryList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.limit = val;
      this.getSoftwareCategoryList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.getSoftwareCategoryList();
    },
  },
};
</script>

<style scoped>
.container {
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.query-container {
  margin-bottom: 20px;
}

.query-card {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.table-container {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  background-color: white;
}

.pagination-container {
  margin-top: 20px;
}
</style>
